﻿using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Dysmsapi.Model.V20170525;

namespace StudyCompanion.Core.Aliyun;

public class Sms
{
    //签名
    private const string signName = "迪娅尔";

    //模板编码
    private const string smsCodeTemp = "SMS_265650303";

    public Sms()
    {
        accessKeyId = "LTAI5tHjLF7rTU9qPnQ74Dcu";
        accessKeySecret = "nyI9iYwShfhSNSSGOvoTIYE2PItuxC";
    }

    /// <summary>
    ///     获取阿里云 accessKeyId
    /// </summary>
    public string accessKeyId { get; set; }

    /// <summary>
    ///     获取阿里云 accessKeySecret
    /// </summary>
    public string accessKeySecret { get; set; }

    /// <summary>
    ///     获取阿里云 短信签名
    /// </summary>
    public string signature { get; set; }

    //产品名称:云通信短信API产品,开发者无需替换
    public string product { get; } = "Dysmsapi";

    //产品域名,开发者无需替换
    public string domain { get; } = "dysmsapi.aliyuncs.com";


    /// <summary>
    ///     发送验证码
    /// </summary>
    /// <param name="tele">电话号码</param>
    /// <param name="smsCodeTempJson">模板对应内容</param>
    /// <returns></returns>
    public Tuple<bool, string> SendSms(string tele, string smsCodeTempJson)
    {
        try
        {
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, accessKeySecret);
            profile.AddEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);
            var request = new SendSmsRequest
            {
                //必填:待发送手机号。支持以逗号分隔的形式进行批量调用，批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
                PhoneNumbers = tele,
                //必填:短信签名-可在短信控制台中找到
                SignName = signName,
                //必填:短信模板-可在短信控制台中找到
                TemplateCode = smsCodeTemp,
                //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
                TemplateParam = smsCodeTempJson,
                //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
                OutId = "yourOutId"
            };
            //请求失败这里会抛ClientException异常
            var res = acsClient.GetAcsResponse(request);
            return res.Code switch
            {
                "OK" => new Tuple<bool, string>(true, "发送成功"),
                "isp.RAM_PERMISSION_DENY" => //RAM权限DENY
                    new Tuple<bool, string>(false, "RAM权限DENY"),
                "isv.OUT_OF_SERVICE" => //业务停机
                    new Tuple<bool, string>(false, "业务停机"),
                "isv.PRODUCT_UN_SUBSCRIPT" => //未开通云通信产品的阿里云客户
                    new Tuple<bool, string>(false, "未开通云通信产品的阿里云客户"),
                "isv.PRODUCT_UNSUBSCRIBE" => //产品未开通
                    new Tuple<bool, string>(false, "产品未开通"),
                "isv.ACCOUNT_NOT_EXISTS" => //账户不存在
                    new Tuple<bool, string>(false, "账户不存在"),
                "isv.ACCOUNT_ABNORMAL" => //账户异常
                    new Tuple<bool, string>(false, "账户异常"),
                "isv.SMS_TEMPLATE_ILLEGAL" => //短信模板不合法
                    new Tuple<bool, string>(false, "短信模板不合法"),
                "isv.SMS_SIGNATURE_ILLEGAL" => //短信签名不合法
                    new Tuple<bool, string>(false, "短信签名不合法"),
                "isv.INVALID_PARAMETERS" => //参数异常
                    new Tuple<bool, string>(false, "参数异常"),
                "isp.SYSTEM_ERROR" => //系统错误
                    new Tuple<bool, string>(false, "系统错误"),
                "isv.MOBILE_NUMBER_ILLEGAL" => //非法手机号
                    new Tuple<bool, string>(false, "非法手机号"),
                "isv.MOBILE_COUNT_OVER_LIMIT" => //手机号码数量超过限制
                    new Tuple<bool, string>(false, "手机号码数量超过限制"),
                "isv.TEMPLATE_MISSING_PARAMETERS" => //模板缺少变量
                    new Tuple<bool, string>(false, "模板缺少变量"),
                "isv.BUSINESS_LIMIT_CONTROL" => //业务限流
                    new Tuple<bool, string>(false, "业务限流"),
                "isv.INVALID_JSON_PARAM" => //JSON参数不合法，只接受字符串值
                    new Tuple<bool, string>(false, "JSON参数不合法，只接受字符串值"),
                "isv.BLACK_KEY_CONTROL_LIMIT" => //黑名单管控
                    new Tuple<bool, string>(false, "黑名单管控"),
                "isv.PARAM_LENGTH_LIMIT" => //参数超出长度限制
                    new Tuple<bool, string>(false, "参数超出长度限制"),
                "isv.PARAM_NOT_SUPPORT_URL" => //不支持URL
                    new Tuple<bool, string>(false, "不支持URL"),
                "isv.AMOUNT_NOT_ENOUGH" => //账户余额不足
                    new Tuple<bool, string>(false, "账户余额不足"),
                _ => new Tuple<bool, string>(false, "其它异常")
            };
        }
        catch (Exception e)
        {
            return new Tuple<bool, string>(false, e.Message);
        }
    }
}